module XI.Directory.WS.TypesData

import Data.Time.Calendar
import Data.Typeable
import Data.Data
import Data.Fixed

instance Typeable (Fixed a) where
  typeOf lt = mkTyConApp (mkTyCon "Fixed") []

instance Typeable E12 where
  typeOf lt = mkTyConApp (mkTyCon "E12") []

instance Typeable Day where
  typeOf lt = mkTyConApp (mkTyCon "ModifiedJulianDay") []

instance Typeable TimeOfDay where
  typeOf lt = mkTyConApp (mkTyCon "TimeOfDay") []

picoType :: DataType
picoType = mkFloatType "Prelude.Fixed"

instance Data (Fixed E12) where
	toConstr x = mkFloatConstr picoType (realToFrac x)
	dataTypeOf _ = picoType
	gunfold _ z c = case constrRep c of
		(FloatConstr x) -> z (realToFrac x)
		_ -> error "gunfold"

localTimeConstr :: Constr
localTimeConstr = mkConstr localTimeDataType "LocalTime" [] Prefix

localTimeDataType :: DataType
localTimeDataType = mkDataType "Data.Time.LocalTime" [localTimeConstr]

instance Data LocalTime where
	gunfold k z c = case constrIndex c of
                         1 -> k (k (z LocalTime))
	gfoldl f z (LocalTime a b) = z LocalTime `f` a `f` b
	toConstr _ = localTimeConstr
	dataTypeOf _ = localTimeDataType
	
dayConstr :: Constr
dayConstr = mkConstr dayDataType "ModifiedJulianDay" [] Prefix

dayDataType :: DataType
dayDataType = mkDataType "Data.Time.Calendar.Day" [dayConstr]

instance Data Day where
	gunfold k z c = case constrIndex c of
                         1 -> k (z ModifiedJulianDay)
	gfoldl f z (ModifiedJulianDay d) = z ModifiedJulianDay `f` d
	toConstr _ = dayConstr
	dataTypeOf _ = dayDataType
	
timeOfDayConstr :: Constr
timeOfDayConstr = mkConstr timeOfDayDataType "TimeOfDay" [] Prefix

timeOfDayDataType :: DataType
timeOfDayDataType = mkDataType "Data.Time.LocalTime.TimeOfDay" [timeOfDayConstr]

instance Data TimeOfDay where
	gunfold k z c = case constrIndex c of
                         1 -> k (k (k (z TimeOfDay)))
	gfoldl f z (TimeOfDay a b c) = z TimeOfDay `f` a `f` b `f` c
	toConstr _ = timeOfDayConstr
	dataTypeOf _ = timeOfDayDataType

